package com.tfleo.mealssenger.adapter;

import java.util.List;

import com.github.ignition.core.widgets.RemoteImageView;
import com.tfleo.mealssenger.FoodActivity;
import com.tfleo.mealssenger.PlaceActivity;
import com.tfleo.mealssenger.UserActivity;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.object.News;
import com.tfleo.mealssenger.utility.IntentExtras;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class NewsListAdapter extends ArrayAdapter<News> {
	private static final String CHECKIN_NEWS = "has checked in to ";
	private static final String LIKES_NEWS = "mealed up ";
	private LayoutInflater mInflater;
	private Context mContext;
	
	public NewsListAdapter(Context context, int resource, int textViewResourceId, 
			List<News> objects, LayoutInflater inflater) {
		super(context, resource, textViewResourceId, objects);
		mInflater = inflater;
		mContext = context;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {   
		ViewHolder holder = null;
		TextView name = null;
		TextView date = null;
		TextView story = null;
		RemoteImageView image = null;
		
		final News news = getItem(position);

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.news_entry, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		}

		holder = (ViewHolder) convertView.getTag();

		name = holder.getNameView();
		name.setText(news.userName);
		name.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) {
	        	Intent i = new Intent(mContext, UserActivity.class);
	        	i.putExtra("who", UserActivity.IS_FRIEND);
	        	i.putExtra("id", news.userId);
            	mContext.startActivity(i); 
	        }
	    });
		

		story = holder.getStoryView();
		if(news.storyType == 1) {
			story.setText(Html.fromHtml(CHECKIN_NEWS + "<b>" + news.storyName + "</b>"));
			story.setOnClickListener(new OnClickListener() {
		        public void onClick(View v) {
		        	Intent i = new Intent(mContext, PlaceActivity.class);
		        	i.putExtra(IntentExtras.PLACE_ID, news.storyId);
	            	mContext.startActivity(i); 
		        }
		    });
		} else {
			story.setText(Html.fromHtml(LIKES_NEWS + "<b>" + news.storyName + "</b>"));
			story.setOnClickListener(new OnClickListener() {
		        public void onClick(View v) {
		        	Intent i = new Intent(mContext, FoodActivity.class);
		        	i.putExtra("id", news.storyId);
	            	mContext.startActivity(i); 
		        }
		    });
		}
		
		date = holder.getDateView();
		date.setText(news.date);
		
		image = holder.getImageView();
		image.setImageUrl(news.userImageUrl);
		image.loadImage();
		

		return convertView;
	}
	
	private class ViewHolder {
		private View vRow;
		private TextView vName = null;
		private TextView vDate = null;
		private TextView vStory = null;
		private RemoteImageView vImage = null;

		public ViewHolder(View row) {
			vRow = row;
		}

		public TextView getNameView() {
			if (vName == null) {
				vName = (TextView) vRow.findViewById(R.id.name);
			}
			return vName;
		}
		
		public TextView getDateView() {
			if(vDate == null) {
				vDate = (TextView) vRow.findViewById(R.id.date);
			}
			return vDate;
		}
		
		public TextView getStoryView() {
			if (vStory == null) {
				vStory = (TextView) vRow.findViewById(R.id.story);
			}
			return vStory;
		}

		public RemoteImageView getImageView() {
			if (vImage == null) {
				vImage = (RemoteImageView) vRow.findViewById(R.id.img);
			}
			return vImage;
		}
	}
}
